package exporter

import (
	"emc_vnx_exporter/utils"
	"github.com/prometheus/client_golang/prometheus"
	"io/ioutil"
	"log"
	"os/exec"
	"strings"
	"time"
)

type Lun struct {
	Name                 string
	State                string
	RAIDGroupID          string
	UID                  string
	DefaultOwner         string
	PrctBound            string
	VerifyPriority       string
	WriteCache           string
	ReadCache            string
	LUNCapacityMegabytes string
	LUNCapacityBlocks    string
}

var (
	lun = prometheus.NewGaugeVec(
		prometheus.GaugeOpts{
			Name: "vnx_get_all_lun",
			Help: "Get All Lun Info",
		},
		[]string{"Name", "State", "RAIDGroupId", "UID", "DefaultOwner", "PrctBound", "VerifyPriority", "WriteCache", "ReadCache", "LUNCapacityMegabytes", "LUNCapacityBlocks"},
	)
)

func LunGauge() *prometheus.GaugeVec {
	return lun
}

func GetAllLunMetrics(spIp string, user string, password string) {
	for {
		cmd := exec.Command("bash", "-c", "/opt/Navisphere/bin/naviseccli -h "+spIp+" -user "+user+" -password "+password+" -Scope 0 getlun -name -rg -uid -default -bind -verify -state -wc -rc -capacity")
		stdout, _ := cmd.StdoutPipe()
		defer stdout.Close()
		if err := cmd.Start(); err != nil {
			log.Fatalf("cmd.Start: %v", err)
		}
		result, _ := ioutil.ReadAll(stdout) // 读取输出结果
		resData := string(result)

		lunList := strings.Split(resData, "\n\n")
		for _, lunString := range lunList {
			lunItems := strings.Split(lunString, "\n")
			data := new(Lun)
			for _, lunItem := range lunItems {
				if lunItem == "" {
					continue
				}

				value := ""
				if !strings.Contains(lunItem, ":") {
					if !strings.Contains(lunItem, "Name") {
						continue
					} else {
						value = strings.Replace(lunItem, "Name", "", -1)
						value = strings.Replace(value, "  ", "", -1)
						data.Name = value
					}
				} else {
					value = lunItem[strings.Index(lunItem, ":")+1 : strings.Count(lunItem, "")-1]
					value = strings.Replace(value, "  ", "", -1)
				}

				if strings.Contains(lunItem, "RAIDGroup ID") {
					data.RAIDGroupID = value
				} else if strings.Contains(lunItem, "UID") {
					data.UID = value
				} else if strings.Contains(lunItem, "Default Owner") {
					data.DefaultOwner = value
				} else if strings.Contains(lunItem, "Prct Bound") {
					data.PrctBound = value
				} else if strings.Contains(lunItem, "Verify Priority") {
					data.VerifyPriority = value
				} else if strings.Contains(lunItem, "State") {
					data.State = value
				} else if strings.Contains(lunItem, "Write cache") {
					data.WriteCache = value
				} else if strings.Contains(lunItem, "Read cache") {
					data.ReadCache = value
				} else if strings.Contains(lunItem, "LUN Capacity(Megabytes)") {
					data.LUNCapacityMegabytes = value
				} else if strings.Contains(lunItem, "LUN Capacity(Blocks)") {
					data.LUNCapacityBlocks = value
				}
			}
			if data.Name != "" {
				lun.WithLabelValues(
					data.Name,
					data.State,
					data.RAIDGroupID,
					data.UID,
					data.DefaultOwner,
					data.PrctBound,
					data.VerifyPriority,
					data.WriteCache,
					data.ReadCache,
					data.LUNCapacityMegabytes,
					data.LUNCapacityBlocks,
				).Set(utils.LunStateToFloat(data.State))
			}
		}

		time.Sleep(60 * time.Second)
	}
}
